package kgc.crm.test;

import java.util.concurrent.locks.ReentrantLock;

//Lock锁
public class MpTest2 {
    public static void main(String[] args) {
        Mp mp = new Mp();
        new Thread(mp).start();//一个线程
        new Thread(mp).start();//一个线程
        new Thread(mp).start();//一个线程
    }
}

class Mp implements Runnable {
        int ps = 10;
        //定义lock锁 是juc包下的一个接口
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public  void run() {
        while (true) {
            try {
                lock.lock();//加锁
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ps > 0) {
                    System.out.println(Thread.currentThread().getName()+"\n"+ps--);
                }else
                    break;
            }finally {
                lock.unlock();//解锁
            }
        }
    }
}
